<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('coupons.coupons.Coupons Name') })"
        />

        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" />

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { ref, provide } from 'vue'
import baTableClass from '/@/utils/baTable'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'

defineOptions({
    name: 'user/coupons',
})

const { t } = useI18n()
const tableRef = ref()
const baTable = new baTableClass(
    new baTableApi('/admin/coupons/'),
    {
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('Id'), prop: 'id', align: 'center', operator: '=', operatorPlaceholder: t('Id') },
            { label: t('coupons.coupons.code'), prop: 'code', align: 'center', operator: '=', operatorPlaceholder: t('code') },
            {
                label: t('coupons.coupons.title'),
                prop: 'coupons_title',
                align: 'center',
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                showOverflowTooltip: true,
            },
            {
                label: t('coupons.coupons.Coupons Type'),
                prop: 'type',
                align: 'center',
                render: 'tag',
                custom: { reduction: 'info', discount: 'success' },
                replaceValue: { reduction: t('coupons.coupons.reduction'), discount: t('coupons.coupons.discount') },
            },
            { label: t('coupons.coupons.amount'), prop: 'amount', align: 'center', operator: '=', operatorPlaceholder: t('amount') },
            { label: t('coupons.coupons.min_order'), prop: 'min_order', align: 'center', operator: '=', operatorPlaceholder: t('min_order') },
            { label: t('coupons.coupons.min_reduce'), prop: 'min_reduce', align: 'center', operator: '=', operatorPlaceholder: t('min_reduce') },
            {
                label: t('coupons.coupons.start_date'),
                prop: 'start_date',
                align: 'center',
                operator: '=',
                operatorPlaceholder: t('start_date'),
                showOverflowTooltip: true,
            },
            {
                label: t('coupons.coupons.end_date'),
                prop: 'end_date',
                align: 'center',
                operator: '=',
                operatorPlaceholder: t('end_date'),
                showOverflowTooltip: true,
            },
            { label: t('coupons.coupons.usage_limit'), prop: 'usage_limit', align: 'center', operator: '=', operatorPlaceholder: t('usage_limit') },
            {
                label: t('State'),
                prop: 'status',
                align: 'center',
                render: 'tag',
                custom: { valid: 'success', used: 'danger', expired: 'warning', disabled: 'info' },
                replaceValue: {
                    valid: t('coupons.coupons.valid'),
                    used: t('coupons.coupons.used'),
                    expired: t('coupons.coupons.expired'),
                    disabled: t('coupons.coupons.disabled'),
                },
            },
            {
                label: t('Operate'),
                align: 'center',
                width: '100',
                render: 'buttons',
                buttons: defaultOptButtons(['edit', 'delete']),
                operator: false,
            },
        ],
        dblClickNotEditColumn: [undefined],
    },
    {
        defaultItems: {
            type: 'reduction',
            usage_limit: '0',
            status: 'valid',
        },
    }
)

baTable.mount()
baTable.getIndex()

provide('baTable', baTable)
</script>

<style scoped lang="scss"></style>
